<template>
  <el-container class="home_container">
    <el-header>
      <div class="logo">
        <img src="../assets/heima.png" alt="" />
        <span>杭州9期后台管理</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <!-- 内容的左侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 切换菜单折叠 -->
        <div class="toggleMenu" @click="isCollapse = !isCollapse">|||</div>
        <!-- 左侧菜单 -->
        <el-menu
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#409eff"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 可以展开的一级菜单 -->
          <el-submenu
            :index="item.id + ''"
            v-for="item in menusList"
            :key="item.id"
          >
            <!-- 定义一级菜单的展示效果 -->
            <template slot="title">
              <i :class="iconsList[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单不需要展开 -->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <!-- 定义二级菜单的展示效果 -->
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容的右侧主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menusList: [],
      iconsList: {
        "125": "iconfont icon-users",
        "103": "iconfont icon-tijikongjian",
        "101": "iconfont icon-shangpin",
        "102": "iconfont icon-danju",
        "145": "iconfont icon-baobiao"
      },
      //是否折叠
      isCollapse: false,
      //被激活的链接地址
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = sessionStorage.getItem("activePath");
  },

  methods: {
    //退出定义事件函数，清除token并回到登录页

    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },

    //获取左侧菜单
    async getMenuList() {
      const { data: res } = await this.$axios("menus");
      if (res.meta.status !== 200) return this.$message.error("获取数据失败");
      this.menusList = res.data;
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style scoped>
.home_container {
  height: 100%;
}
.el-header {
  background: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.el-menu {
  border-right: none;
}
.logo {
  display: flex;
  align-items: center;
}
.logo span {
  color: #fff;
  font-size: 20px;
  padding-left: 25px;
}
.el-aside {
  background: #373d41;
}
.el-main {
  background: #eaedf1;
}
.iconfont {
  margin-right: 10px;
}
.toggleMenu {
  width: 100%;
  height: 30px;
  line-height: 30px;
  text-align: center;
  color: #fff;
  background: green;
  letter-spacing: 8px;
  cursor: pointer;
  user-select: none;
}
</style>
